<?php

class Student_CourseController extends Zend_Controller_Action
{

   // $user_id = Zend_Auth::getInstance()->getIdentity()->id;

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
       $this->view->title = "My Courses";
       $this->view->headTitle($this->view->title, 'PREPEND');

       $courses = new Student_Model_DbTable_Course();
       $user_id = Zend_Auth::getInstance()->getIdentity()->id;
       $allcourses = $courses->getSingleUserCourses($user_id);
       $this->view->courses = $allcourses;

       
    }

    public function addAction()
    {
       $this->view->title = "Add a Course";
       //$this->view->headTitle($this->view->title, 'PREPEND');
        $form = new Student_Form_NewCourse();
        $this->view->form = $form;

        if ($this->getRequest()->isPost()){
    		$formData = $this->getRequest()->getPost();
    		if($form->isValid($formData))
                        {
    			$coursename = $form->getValue('course_name');
                        $coursenumber = $form->getValue('course_number');
                        $coursecredits = $form->getValue('course_credits');
                        $desiredgrade = $form->getValue('desired_grade');
                        $semester = $form->getValue('semester');
                        $year = $form->getValue('year');
                        $user_id = Zend_Auth::getInstance()->getIdentity()->id;
                        $add = new Student_Model_DbTable_Course();
    			$add->addCourse($coursename, $coursenumber, $coursecredits, $desiredgrade, $semester, $year, $user_id);
    			$this->view->message = "Course $coursenumber has been saved on your records.";
                        $this->_redirect('/student/course/index');
                        }
                else
                    {
    			$form->populate($formData);
                    }
    }
    }


    public function editAction()
    {
        $this->view->title = "Edit Course";
        $this->view->headTitle($this->view->title);
        $form = new Student_Form_NewCourse();
        $this->view->form = $form;
        if ($this->getRequest()->isPost())
        {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = $this->_getParam('id');
                $course_name = $form->getValue('course_name');
                $course_number = $form->getValue('course_number');
                $course_credits = $form->getValue('course_credits');
                $desired_grade = $form->getValue('desired_grade');
                $semester = $form->getValue('semester');
                $year = $form->getValue('year');
                //$user_id = $form->getValue('user_id');
                $courses = new Student_Model_DbTable_Course();

                 //$valid = $form->getValues();
                // print_r($valid);
                 //      echo $id;
                 //      echo $user_id;
                $courses->updateCourse($id,$course_name, $course_number, $course_credits, $desired_grade, $semester, $year);
                $this->_redirect('/student/course/index');
                }
                else
                {
                    $form->populate($formData);
                }
        }
        else
        {
            $id = $this->_getParam('id', 0);
            if ($id > 0)
            {
                $courses = new Student_Model_DbTable_Course();
                $form->populate($courses->getCourse($id));
            }
        }
    }
    

    public function viewAction()
    {
       
       $id = $this->_getParam('id', 0);
       $courses = new Student_Model_DbTable_Course();
       $course_details = $courses->getCourseDetails($id);

       //$course_id = $this->_getParam('id', 0);
      // $courses = new Student_Model_DbTable_Course();
       
       $this->view->courses = $course_details;
    }

    public function deleteAction()
    {
        $this->view->title = "Delete Course";
        $this->view->headTitle($this->view->title, 'PREPEND');
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('id');
                $course = new Student_Model_DbTable_Course();
               // echo $id;
                $course->deleteCourse($id);


            }
            $this->_redirect('/student/course/index');
            } else {

                //
             $id = $this->_getParam('id', 0);
             $courses = new Student_Model_DbTable_Course();
             $test = $courses->getCourse($id);
              //print_r($test);
              $this->view->course = $test;

}
    }



}

